﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.facebook.graph {
    import flash.net.*;
    import flash.utils.*;
    import com.facebook.graph.data.*;
    import com.adobe.serialization.json.*;
    import com.facebook.graph.core.*;
    import flash.external.*;

    public class Facebook extends AbstractFacebook {

        protected var jsCallbacks:Object;
        protected var applicationId:String;
        protected var _logoutCallback:Function;
        protected var _loginCallback:Function;
        protected var openUICalls:Dictionary;
        protected var _initCallback:Function;
        protected var jsBridge:FacebookJSBridge;

        protected static var _canInit:Boolean = false;
        protected static var _instance:Facebook;

        public function Facebook(){
            if (_canInit == false){
                throw (new Error("Facebook is an singleton and cannot be instantiated."));
            };
            jsBridge = new FacebookJSBridge();
            jsCallbacks = {};
            openUICalls = new Dictionary();
        }
        protected function getLoginStatus():void{
            ExternalInterface.call("FBAS.getLoginStatus");
        }
        protected function init(_arg1:String, _arg2:Function=null, _arg3:Object=null, _arg4:String=null):void{
            ExternalInterface.addCallback("handleJsEvent", handleJSEvent);
            ExternalInterface.addCallback("sessionChange", handleSessionChange);
            ExternalInterface.addCallback("logout", handleLogout);
            ExternalInterface.addCallback("uiResponse", handleUI);
            _initCallback = _arg2;
            this.applicationId = _arg1;
            if (_arg3 == null){
                _arg3 = {};
            };
            _arg3.appId = _arg1;
            ExternalInterface.call("FBAS.init", JSON.encode(_arg3));
            if (_arg4 != null){
                session = new FacebookSession();
                session.accessToken = _arg4;
            };
            getLoginStatus();
        }
        protected function logout(_arg1:Function):void{
            _logoutCallback = _arg1;
            ExternalInterface.call("FBAS.logout");
        }
        protected function hasJSEventListener(_arg1:String, _arg2:Function):Boolean{
            if ((((jsCallbacks[_arg1] == null)) || (!((jsCallbacks[_arg1][_arg2] === null))))){
                return (false);
            };
            return (true);
        }
        protected function ui(_arg1:String, _arg2:Object, _arg3:Function=null, _arg4:String=null):void{
            _arg2.method = _arg1;
            if (_arg3 != null){
                openUICalls[_arg1] = _arg3;
            };
            if (_arg4){
                _arg2.display = _arg4;
            };
            ExternalInterface.call("FBAS.ui", JSON.encode(_arg2));
        }
        protected function handleLogout():void{
            session = null;
            if (_logoutCallback != null){
                _logoutCallback(true);
                _logoutCallback = null;
            };
        }
        protected function getSession():FacebookSession{
            var sessionObj:Object;
            var result:String = ExternalInterface.call("FBAS.getSession");
            try {
                sessionObj = JSON.decode(result);
            } catch(e) {
                return (null);
            };
            var s:FacebookSession = new FacebookSession();
            s.fromJSON(sessionObj);
            this.session = s;
            return (session);
        }
        protected function removeJSEventListener(_arg1:String, _arg2:Function):void{
            if (jsCallbacks[_arg1] == null){
                return;
            };
            delete jsCallbacks[_arg1][_arg2];
        }
        protected function handleSessionChange(_arg1:String, _arg2:String=null):void{
            var resultObj:Object;
            var result = _arg1;
            var permissions = _arg2;
            var success:Boolean;
            if (result != null){
                try {
                    resultObj = JSON.decode(result);
                } catch(e:JSONParseError) {
                    success = false;
                };
            } else {
                success = false;
            };
            if (success){
                if (session == null){
                    session = new FacebookSession();
                    session.fromJSON(resultObj);
                } else {
                    session.fromJSON(resultObj);
                };
                if (permissions != null){
                    try {
                        session.availablePermissions = JSON.decode(permissions);
                    } catch(e:JSONParseError) {
                        session.availablePermissions = null;
                    };
                };
            };
            if (_initCallback != null){
                _initCallback(session, null);
                _initCallback = null;
            };
            if (_loginCallback != null){
                _loginCallback(session, null);
                _loginCallback = null;
            };
        }
        protected function callJS(_arg1:String, _arg2:Object):void{
            ExternalInterface.call(_arg1, _arg2);
        }
        protected function login(_arg1:Function, _arg2:Object=null):void{
            _loginCallback = _arg1;
            ExternalInterface.call("FBAS.login", JSON.encode(_arg2));
        }
        protected function addJSEventListener(_arg1:String, _arg2:Function):void{
            if (jsCallbacks[_arg1] == null){
                jsCallbacks[_arg1] = new Dictionary();
                ExternalInterface.call("FBAS.addEventListener", _arg1);
            };
            jsCallbacks[_arg1][_arg2] = null;
        }
        protected function handleJSEvent(_arg1:String, _arg2:String=null):void{
            var decodedResult:Object;
            var func:Object;
            var event = _arg1;
            var result = _arg2;
            if (jsCallbacks[event] != null){
                try {
                    decodedResult = JSON.decode(result);
                } catch(e:JSONParseError) {
                };
                for (func in jsCallbacks[event]) {
                    (func as Function)(decodedResult);
                    delete jsCallbacks[event][func];
                };
            };
        }
        protected function setCanvasSize(_arg1:Number, _arg2:Number):void{
            ExternalInterface.call("FBAS.setCanvasSize", _arg1, _arg2);
        }
        protected function handleUI(_arg1:String, _arg2:String):void{
            var _local3:Object = (_arg1) ? JSON.decode(_arg1) : null;
            var _local4:Function = openUICalls[_arg2];
            if (_local4 === null){
                delete openUICalls[_arg2];
            } else {
                _local4(_local3);
                delete openUICalls[_arg2];
            };
        }
        protected function setCanvasAutoResize(_arg1:Boolean=true, _arg2:uint=100):void{
            ExternalInterface.call("FBAS.setCanvasAutoResize", _arg1, _arg2);
        }

        public static function getLoginStatus():void{
            getInstance().getLoginStatus();
        }
        public static function fqlQuery(_arg1:String, _arg2:Function):void{
            getInstance().fqlQuery(_arg1, _arg2);
        }
        public static function logout(_arg1:Function):void{
            getInstance().logout(_arg1);
        }
        public static function hasJSEventListener(_arg1:String, _arg2:Function):Boolean{
            return (getInstance().hasJSEventListener(_arg1, _arg2));
        }
        public static function mobileLogout(_arg1:String):void{
            getInstance().session = null;
            var _local2:URLVariables = new URLVariables();
            _local2.confirm = 1;
            _local2.next = _arg1;
            var _local3:URLRequest = new URLRequest("http://m.facebook.com/logout.php");
            _local3.method = URLRequestMethod.GET;
            _local3.data = _local2;
            navigateToURL(_local3, "_self");
        }
        public static function init(_arg1:String, _arg2:Function=null, _arg3:Object=null, _arg4:String=null):void{
            getInstance().init(_arg1, _arg2, _arg3, _arg4);
        }
        public static function deleteObject(_arg1:String, _arg2:Function=null):void{
            getInstance().deleteObject(_arg1, _arg2);
        }
        protected static function getInstance():Facebook{
            if (_instance == null){
                _canInit = true;
                _instance = new (Facebook);
                _canInit = false;
            };
            return (_instance);
        }
        public static function ui(_arg1:String, _arg2:Object, _arg3:Function=null, _arg4:String=null):void{
            getInstance().ui(_arg1, _arg2, _arg3, _arg4);
        }
        public static function getImageUrl(_arg1:String, _arg2:String=null):String{
            return (getInstance().getImageUrl(_arg1, _arg2));
        }
        public static function api(_arg1:String, _arg2:Function=null, _arg3=null, _arg4:String="GET"):void{
            return (getInstance().api(_arg1, _arg2, _arg3, _arg4));
        }
        public static function getSession():FacebookSession{
            return (getInstance().getSession());
        }
        public static function removeJSEventListener(_arg1:String, _arg2:Function):void{
            getInstance().removeJSEventListener(_arg1, _arg2);
        }
        public static function addJSEventListener(_arg1:String, _arg2:Function):void{
            getInstance().addJSEventListener(_arg1, _arg2);
        }
        public static function mobileLogin(_arg1:String, _arg2:String="touch", _arg3:Array=null):void{
            var _local4:URLVariables = new URLVariables();
            _local4.client_id = getInstance().applicationId;
            _local4.redirect_uri = _arg1;
            _local4.display = _arg2;
            if (_arg3 != null){
                _local4.scope = _arg3.join(",");
            };
            var _local5:URLRequest = new URLRequest(FacebookURLDefaults.AUTH_URL);
            _local5.method = URLRequestMethod.GET;
            _local5.data = _local4;
            navigateToURL(_local5, "_self");
        }
        public static function login(_arg1:Function, _arg2:Object=null):void{
            getInstance().login(_arg1, _arg2);
        }
        public static function postData(_arg1:String, _arg2:Function=null, _arg3:Object=null):void{
            api(_arg1, _arg2, _arg3, URLRequestMethod.POST);
        }
        public static function callJS(_arg1:String, _arg2:Object):void{
            getInstance().callJS(_arg1, _arg2);
        }
        public static function callRestAPI(_arg1:String, _arg2:Function, _arg3=null, _arg4:String="GET"):void{
            return (getInstance().callRestAPI(_arg1, _arg2, _arg3, _arg4));
        }
        public static function setCanvasSize(_arg1:Number, _arg2:Number):void{
            getInstance().setCanvasSize(_arg1, _arg2);
        }
        public static function setCanvasAutoResize(_arg1:Boolean=true, _arg2:uint=100):void{
            getInstance().setCanvasAutoResize(_arg1, _arg2);
        }

    }
}//package com.facebook.graph 
